package com.android.lovingpet.adopt.host.behost;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.common.Model;
import com.android.lovingpet.util.LocationClient;
import com.android.lovingpet.util.MyHttp;
import com.android.lovingpet.util.Utility;

import java.io.File;
import java.util.HashMap;
/**
 * Created by ikata on 2018/4/27.
 */
public class BeHostPresenter extends BasePresenter<IBeHostContract.View> implements IBeHostContract.Presenter {

    private Model model;
    private BeHostModel beHostModel;

    public static final int FLAG_GET_ADDRESS=0;
    public static final int FLAG_BE_HOST=1;
    public static final int FLAG_UPLOAD_IMAGE=2;
    public BeHostPresenter()
    {
        this.model=new Model();
        this.beHostModel=new BeHostModel();
    }
    @Override
    public void beHost(String name, String address, String phone, String description, String service, String attention, String petExp,double longitude,double latitude,File image) {
        HashMap<String,String> params=new HashMap<>();
        params.put("name",name);
        params.put("address",address);
        params.put("phone",phone);
        params.put("description",description);
        params.put("service",service);
        params.put("attention",attention);
        params.put("petExp",petExp);
        params.put("longitude", String.valueOf(longitude));
        params.put("latitude", String.valueOf(latitude));
        beHostModel.beHost(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()==null)
                    return;
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle==null)
                {
                    if (image!=null) {
                        uploadImage(Utility.getRealId(), image);
                    }else {
                        if (getView()==null)
                            return;
                        getView().next(null);
                    }
                }
            }
        });

    }

    public void getAddress()
    {
        LocationClient.getLocation(new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                AMapLocation aMapLocation=bundle.getParcelable("result");
                String latitude= String.valueOf(aMapLocation.getLatitude());
                String longitude=String.valueOf(aMapLocation.getLongitude());
                bundle.putInt("flag",FLAG_GET_ADDRESS);
                if (getView()==null)
                    return;
                getView().next(bundle);
            }
        });
    }


    public void uploadImage(int id,File file)
    {
        FileModel.getInstance().uploadFile(1,id,3,3,file,new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                Log.d("有没有阿","sss");
                getView().next(null);
            }
        });
    }
}
